#include <stdio.h>
#include <stdlib.h>
#include "lt8618_ioctl.h"
#include <string.h>
#include <math.h>

#define DMT_SIZE 0x3C
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define perr(message) fprintf(stderr, "[ERROR] %s\r\n", message)
struct edid_cta_mode
{
    const char *xmodeline;
    const char *name;
    int refresh, hor_freq_hz, pixclk_khz;
};

static struct edid_cta_mode edid_cta_modes1[] = {
    /* VIC 1 */
    {"Modeline \"640x480_60.00\" 25.175 640 656 752 800 480 490 492 525 +HSync +VSync", "640x480@60Hz 4:3", 60, 31469, 25175},
    {"Modeline \"720x480_60.00\" 27 720 736 798 858 480 489 495 525 +HSync +VSync", "720x480@60Hz 4:3", 60, 31469, 27000},
    {"Modeline \"720x480_60.00\" 27 720 736 798 858 480 489 495 525 +HSync +VSync", "720x480@60Hz 16:9", 60, 31469, 27000},
    {"Modeline \"1280x720_60.00\" 74.25 1280 1390 1430 1650 720 725 730 750 +HSync +VSync", "1280x720@60Hz 16:9", 60, 45000, 74250},
    {"Modeline \"1920x1080_60.00\" 74.25 1920 2008 2052 2200 1080 1082 1087 1125 +HSync +VSync", "1920x1080i@60Hz 16:9", 60, 33750, 74250},
    {"Modeline \"1440x480_60.00\" 27 1440 1478 1602 1716 480 484 487 525 +HSync +VSync", "1440x480i@60Hz 4:3", 60, 15734, 27000},
    {"Modeline \"1440x480_60.00\" 27 1440 1478 1602 1716 480 484 487 525 +HSync +VSync", "1440x480i@60Hz 16:9", 60, 15734, 27000},
    {"Modeline \"1440x240_60.00\" 27 1440 1478 1602 1716 240 245 248 263 +HSync +VSync", "1440x240@60Hz 4:3", 60, 15734, 27000},
    {"Modeline \"1440x240_60.00\" 27 1440 1478 1602 1716 240 245 248 263 +HSync +VSync", "1440x240@60Hz 16:9", 60, 15734, 27000},
    {"Modeline \"2880x480_60.00\" 54 2880 2956 3204 3432 480 484 487 525 +HSync +VSync", "2880x480i@60Hz 4:3", 60, 15734, 54000},
    /* VIC 11 */
    {"Modeline \"2880x480_60.00\" 54 2880 2956 3204 3432 480 484 487 525 +HSync +VSync", "2880x480i@60Hz 16:9", 60, 15734, 54000},
    {"Modeline \"2880x240_60.00\" 54 2880 2956 3204 3432 240 245 248 263 +HSync +VSync", "2880x240@60Hz 4:3", 60, 15734, 54000},
    {"Modeline \"2880x240_60.00\" 54 2880 2956 3204 3432 240 245 248 263 +HSync +VSync", "2880x240@60Hz 16:9", 60, 15734, 54000},
    {"Modeline \"1440x480_60.00\" 54 1440 1472 1596 1716 480 489 495 525 +HSync +VSync", "1440x480@60Hz 4:3", 60, 31469, 54000},
    {"Modeline \"1440x480_60.00\" 54 1440 1472 1596 1716 480 489 495 525 +HSync +VSync", "1440x480@60Hz 16:9", 60, 31469, 54000},
    {"Modeline \"1920x1080_60.00\" 148.5 1920 2008 2052 2200 1080 1084 1089 1125 +HSync +VSync", "1920x1080@60Hz 16:9", 60, 67500, 148500},
    {"Modeline \"720x576_50.00\" 27 720 732 796 864 576 581 586 625 +HSync +VSync", "720x576@50Hz 4:3", 50, 31250, 27000},
    {"Modeline \"720x576_50.00\" 27 720 732 796 864 576 581 586 625 +HSync +VSync", "720x576@50Hz 16:9", 50, 31250, 27000},
    {"Modeline \"1280x720_50.00\" 74.25 1280 1720 1760 1980 720 725 730 750 +HSync +VSync", "1280x720@50Hz 16:9", 50, 37500, 74250},
    {"Modeline \"1920x1080_50.00\" 74.25 1920 2448 2492 2640 1080 1082 1087 1125 +HSync +VSync", "1920x1080i@50Hz 16:9", 50, 28125, 74250},
    /* VIC 21 */
    {"Modeline \"1440x576_50.00\" 27 1440 1464 1590 1728 576 578 581 625 +HSync +VSync", "1440x576i@50Hz 4:3", 50, 15625, 27000},
    {"Modeline \"1440x576_50.00\" 27 1440 1464 1590 1728 576 578 581 625 +HSync +VSync", "1440x576i@50Hz 16:9", 50, 15625, 27000},
    {"Modeline \"1440x288_50.00\" 27 1440 1464 1590 1728 288 292 295 314 +HSync +VSync", "1440x288@50Hz 4:3", 50, 15625, 27000},
    {"Modeline \"1440x288_50.00\" 27 1440 1464 1590 1728 288 292 295 314 +HSync +VSync", "1440x288@50Hz 16:9", 50, 15625, 27000},
    {"Modeline \"2880x576_50.00\" 54 2880 2928 3180 3456 576 578 581 625 +HSync +VSync", "2880x576i@50Hz 4:3", 50, 15625, 54000},
    {"Modeline \"2880x576_50.00\" 54 2880 2928 3180 3456 576 578 581 625 +HSync +VSync", "2880x576i@50Hz 16:9", 50, 15625, 54000},
    {"Modeline \"2880x288_50.00\" 54 2880 2928 3180 3456 288 292 295 314 +HSync +VSync", "2880x288@50Hz 4:3", 50, 15625, 54000},
    {"Modeline \"2880x288_50.00\" 54 2880 2928 3180 3456 288 292 295 314 +HSync +VSync", "2880x288@50Hz 16:9", 50, 15625, 54000},
    {"Modeline \"1440x576_50.00\" 54 1440 1464 1592 1728 576 581 586 625 +HSync +VSync", "1440x576@50Hz 4:3", 50, 31250, 54000},
    {"Modeline \"1440x576_50.00\" 54 1440 1464 1592 1728 576 581 586 625 +HSync +VSync", "1440x576@50Hz 16:9", 50, 31250, 54000},
    /* VIC 31 */
    {"Modeline \"1920x1080_50.00\" 148.5 1920 2448 2492 2640 1080 1084 1089 1125 +HSync +VSync", "1920x1080@50Hz 16:9", 50, 56250, 148500},
    {"Modeline \"1920x1080_24.00\" 74.25 1920 2558 2602 2750 1080 1084 1089 1125 +HSync +VSync", "1920x1080@24Hz 16:9", 24, 27000, 74250},
    {"Modeline \"1920x1080_25.00\" 74.25 1920 2448 2492 2640 1080 1084 1089 1125 +HSync +VSync", "1920x1080@25Hz 16:9", 25, 28125, 74250},
    {"Modeline \"1920x1080_30.00\" 74.25 1920 2008 2052 2200 1080 1084 1089 1125 +HSync +VSync", "1920x1080@30Hz 16:9", 30, 33750, 74250},
    {"Modeline \"2880x480_60.00\" 108 2880 2944 3192 3432 480 489 495 525 +HSync +VSync", "2880x480@60Hz 4:3", 60, 31469, 108000},
    {"Modeline \"2880x480_60.00\" 108 2880 2944 3192 3432 480 489 495 525 +HSync +VSync", "2880x480@60Hz 16:9", 60, 31469, 108000},
    {"Modeline \"2880x576_50.00\" 108 2880 2928 3184 3456 576 581 586 625 +HSync +VSync", "2880x576@50Hz 4:3", 50, 31250, 108000},
    {"Modeline \"2880x576_50.00\" 108 2880 2928 3184 3456 576 581 586 625 +HSync +VSync", "2880x576@50Hz 16:9", 50, 31250, 108000},
    {"Modeline \"1920x1080_50.00\" 72 1920 1952 2120 2304 1080 1103 1108 1250 +HSync +VSync", "1920x1080i@50Hz 16:9", 50, 31250, 72000},
    {"Modeline \"1920x1080_100.00\" 148.5 1920 2448 2492 2640 1080 1082 1087 1125 +HSync +VSync", "1920x1080i@100Hz 16:9", 100, 56250, 148500},
    /* VIC 41 */
    {"Modeline \"1280x720_100.00\" 148.5 1280 1720 1760 1980 720 725 730 750 +HSync +VSync", "1280x720@100Hz 16:9", 100, 75000, 148500},
    {"Modeline \"720x576_100.00\" 54 720 732 796 864 576 581 586 625 +HSync +VSync", "720x576@100Hz 4:3", 100, 62500, 54000},
    {"Modeline \"720x576_100.00\" 54 720 732 796 864 576 581 586 625 +HSync +VSync", "720x576@100Hz 16:9", 100, 62500, 54000},
    {"Modeline \"1440x576_100.00\" 54 1440 1464 1590 1728 576 578 581 625 +HSync +VSync", "1440x576@100Hz 4:3", 100, 31250, 54000},
    {"Modeline \"1440x576_100.00\" 54 1440 1464 1590 1728 576 578 581 625 +HSync +VSync", "1440x576@100Hz 16:9", 100, 31250, 54000},
    {"Modeline \"1920x1080_120.00\" 148.5 1920 2008 2052 2200 1080 1082 1087 1125 +HSync +VSync", "1920x1080i@120Hz 16:9", 120, 67500, 148500},
    {"Modeline \"1280x720_120.00\" 148.5 1280 1390 1430 1650 720 725 730 750 +HSync +VSync", "1280x720@120Hz 16:9", 120, 90000, 148500},
    {"Modeline \"720x480_120.00\" 54 720 736 798 858 480 489 495 525 +HSync +VSync", "720x480@120Hz 4:3", 120, 62937, 54000},
    {"Modeline \"720x480_120.00\" 54 720 736 798 858 480 489 495 525 +HSync +VSync", "720x480@120Hz 16:9", 120, 62937, 54000},
    {"Modeline \"1440x480_120.00\" 54 1440 1478 1602 1716 480 484 487 525 +HSync +VSync", "1440x480i@120Hz 4:3", 120, 31469, 54000},
    /* VIC 51 */
    {"Modeline \"1440x480_120.00\" 54 1440 1478 1602 1716 480 484 487 525 +HSync +VSync", "1440x480i@120Hz 16:9", 120, 31469, 54000},
    {"Modeline \"720x576_200.00\" 108 720 732 796 864 576 581 586 625 +HSync +VSync", "720x576@200Hz 4:3", 200, 125000, 108000},
    {"Modeline \"720x576_200.00\" 108 720 732 796 864 576 581 586 625 +HSync +VSync", "720x576@200Hz 16:9", 200, 125000, 108000},
    {"Modeline \"1440x576_200.00\" 108 1440 1464 1590 1728 576 578 581 625 +HSync +VSync", "1440x576i@200Hz 4:3", 200, 62500, 108000},
    {"Modeline \"1440x576_200.00\" 108 1440 1464 1590 1728 576 578 581 625 +HSync +VSync", "1440x576i@200Hz 16:9", 200, 62500, 108000},
    {"Modeline \"720x480_240.00\" 108 720 736 798 858 480 489 495 525 +HSync +VSync", "720x480@240Hz 4:3", 240, 125874, 108000},
    {"Modeline \"720x480_240.00\" 108 720 736 798 858 480 489 495 525 +HSync +VSync", "720x480@240Hz 16:9", 240, 125874, 108000},
    {"Modeline \"1440x480_240.00\" 108 1440 1478 1602 1716 480 484 487 525 +HSync +VSync", "1440x480i@240Hz 4:3", 240, 62937, 108000},
    {"Modeline \"1440x480_240.00\" 108 1440 1478 1602 1716 480 484 487 525 +HSync +VSync", "1440x480i@240Hz 16:9", 240, 62937, 108000},
    {"Modeline \"1280x720_24.00\" 59.4 1280 3040 3080 3300 720 725 730 750 +HSync +VSync", "1280x720@24Hz 16:9", 24, 18000, 59400},
    /* VIC 61 */
    {"Modeline \"1280x720_25.00\" 74.25 1280 3700 3740 3960 720 725 730 750 +HSync +VSync", "1280x720@25Hz 16:9", 25, 18750, 74250},
    {"Modeline \"1280x720_30.00\" 74.25 1280 3040 3080 3300 720 725 730 750 +HSync +VSync", "1280x720@30Hz 16:9", 30, 22500, 74250},
    {"Modeline \"1920x1080_120.00\" 297 1920 2008 2052 2200 1080 1084 1089 1125 +HSync +VSync", "1920x1080@120Hz 16:9", 120, 135000, 297000},
    {"Modeline \"1920x1080_100.00\" 297 1920 2448 2492 2640 1080 1084 1089 1125 +HSync +VSync", "1920x1080@100Hz 16:9", 100, 112500, 297000},
    {"Modeline \"1280x720_24.00\" 59.4 1280 3040 3080 3300 720 725 730 750 +HSync +VSync", "1280x720@24Hz 64:27", 24, 18000, 59400},
    {"Modeline \"1280x720_25.00\" 74.25 1280 3700 3740 3960 720 725 730 750 +HSync +VSync", "1280x720@25Hz 64:27", 25, 18750, 74250},
    {"Modeline \"1280x720_30.00\" 74.25 1280 3040 3080 3300 720 725 730 750 +HSync +VSync", "1280x720@30Hz 64:27", 30, 22500, 74250},
    {"Modeline \"1280x720_50.00\" 74.25 1280 1720 1760 1980 720 725 730 750 +HSync +VSync", "1280x720@50Hz 64:27", 50, 37500, 74250},
    {"Modeline \"1280x720_60.00\" 74.25 1280 1390 1430 1650 720 725 730 750 +HSync +VSync", "1280x720@60Hz 64:27", 60, 45000, 74250},
    {"Modeline \"1280x720_100.00\" 148.5 1280 1720 1760 1980 720 725 730 750 +HSync +VSync", "1280x720@100Hz 64:27", 100, 75000, 148500},
    /* VIC 71 */
    {"Modeline \"1280x720_120.00\" 148.5 1280 1390 1430 1650 720 725 730 750 +HSync +VSync", "1280x720@120Hz 64:27", 120, 91000, 148500},
    {"Modeline \"1920x1080_24.00\" 74.25 1920 2558 2602 2750 1080 1084 1089 1125 +HSync +VSync", "1920x1080@24Hz 64:27", 24, 27000, 74250},
    {"Modeline \"1920x1080_25.00\" 74.25 1920 2448 2492 2640 1080 1084 1089 1125 +HSync +VSync", "1920x1080@25Hz 64:27", 25, 28125, 74250},
    {"Modeline \"1920x1080_30.00\" 74.25 1920 2008 2052 2200 1080 1084 1089 1125 +HSync +VSync", "1920x1080@30Hz 64:27", 30, 33750, 74250},
    {"Modeline \"1920x1080_50.00\" 148.5 1920 2448 2492 2640 1080 1084 1089 1125 +HSync +VSync", "1920x1080@50Hz 64:27", 50, 56250, 148500},
    {"Modeline \"1920x1080_60.00\" 148.5 1920 2008 2052 2200 1080 1084 1089 1125 +HSync +VSync", "1920x1080@60Hz 64:27", 60, 67500, 148500},
    {"Modeline \"1920x1080_100.00\" 297 1920 2448 2492 2640 1080 1084 1089 1125 +HSync +VSync", "1920x1080@100Hz 64:27", 100, 112500, 297000},
    {"Modeline \"1920x1080_120.00\" 297 1920 2008 2052 2200 1080 1084 1089 1125 +HSync +VSync", "1920x1080@120Hz 64:27", 120, 135000, 297000},
    {"Modeline \"1680x720_24.00\" 59.4 1680 3040 3080 3300 720 725 730 750 +HSync +VSync", "1680x720@24Hz 64:27", 24, 18000, 59400},
    {"Modeline \"1680x720_25.00\" 59.4 1680 2908 2948 3168 720 725 730 750 +HSync +VSync", "1680x720@25Hz 64:27", 25, 18750, 59400},
    /* VIC 81 */
    {"Modeline \"1680x720_30.00\" 59.4 1680 2380 2420 2640 720 725 730 750 +HSync +VSync", "1680x720@30Hz 64:27", 30, 22500, 59400},
    {"Modeline \"1680x720_50.00\" 82.5 1680 1940 1980 2200 720 725 730 750 +HSync +VSync", "1680x720@50Hz 64:27", 50, 37500, 82500},
    {"Modeline \"1680x720_60.00\" 99 1680 1940 1980 2200 720 725 730 750 +HSync +VSync", "1680x720@60Hz 64:27", 60, 45000, 99000},
    {"Modeline \"1680x720_100.00\" 165 1680 1740 1780 2000 720 725 730 825 +HSync +VSync", "1680x720@100Hz 64:27", 100, 82500, 165000},
    {"Modeline \"1680x720_120.00\" 198 1680 1740 1780 2000 720 725 730 825 +HSync +VSync", "1680x720@120Hz 64:27", 120, 99000, 198000},
    {"Modeline \"2560x1080_24.00\" 99 2560 3558 3602 3750 1080 1084 1089 1100 +HSync +VSync", "2560x1080@24Hz 64:27", 24, 26400, 99000},
    {"Modeline \"2560x1080_25.00\" 90 2560 3008 3052 3200 1080 1084 1089 1125 +HSync +VSync", "2560x1080@25Hz 64:27", 25, 28125, 90000},
    {"Modeline \"2560x1080_30.00\" 118.8 2560 3328 3372 3520 1080 1084 1089 1125 +HSync +VSync", "2560x1080@30Hz 64:27", 30, 33750, 118800},
    {"Modeline \"2560x1080_50.00\" 185.625 2560 3108 3152 3300 1080 1084 1089 1125 +HSync +VSync", "2560x1080@50Hz 64:27", 50, 56250, 185625},
    {"Modeline \"2560x1080_60.00\" 198 2560 2808 2852 3000 1080 1084 1089 1100 +HSync +VSync", "2560x1080@60Hz 64:27", 60, 66000, 198000},
    /* VIC 91 */
    {"Modeline \"2560x1080_100.00\" 371.25 2560 2778 2822 2970 1080 1084 1089 1250 +HSync +VSync", "2560x1080@100Hz 64:27", 100, 125000, 371250},
    {"Modeline \"2560x1080_120.00\" 495 2560 3108 3152 3300 1080 1084 1089 1250 +HSync +VSync", "2560x1080@120Hz 64:27", 120, 150000, 495000},
    {"Modeline \"3840x2160_24.00\" 297 3840 5116 5204 5500 2160 2168 2178 2250 +HSync +VSync", "3840x2160@24Hz 16:9", 24, 54000, 297000},
    {"Modeline \"3840x2160_25.00\" 297 3840 4896 4984 5280 2160 2168 2178 2250 +HSync +VSync", "3840x2160@25Hz 16:9", 25, 56250, 297000},
    {"Modeline \"3840x2160_30.00\" 297 3840 4016 4104 4400 2160 2168 2178 2250 +HSync +VSync", "3840x2160@30Hz 16:9", 30, 67500, 297000},
    {"Modeline \"3840x2160_50.00\" 594 3840 4896 4984 5280 2160 2168 2178 2250 +HSync +VSync", "3840x2160@50Hz 16:9", 50, 112500, 594000},
    {"Modeline \"3840x2160_60.00\" 594 3840 4016 4104 4400 2160 2168 2178 2250 +HSync +VSync", "3840x2160@60Hz 16:9", 60, 135000, 594000},
    {"Modeline \"4096x2160_24.00\" 297 4096 5116 5204 5500 2160 2168 2178 2250 +HSync +VSync", "4096x2160@24Hz 256:135", 24, 54000, 297000},
    {"Modeline \"4096x2160_25.00\" 297 4096 5064 5152 5280 2160 2168 2178 2250 +HSync +VSync", "4096x2160@25Hz 256:135", 25, 56250, 297000},
    {"Modeline \"4096x2160_30.00\" 297 4096 4184 4272 4400 2160 2168 2178 2250 +HSync +VSync", "4096x2160@30Hz 256:135", 30, 67500, 297000},
    /* VIC 101 */
    {"Modeline \"4096x2160_50.00\" 594 4096 5064 5152 5280 2160 2168 2178 2250 +HSync +VSync", "4096x2160@50Hz 256:135", 50, 112500, 594000},
    {"Modeline \"4096x2160_60.00\" 594 4096 4184 4272 4400 2160 2168 2178 2250 +HSync +VSync", "4096x2160@60Hz 256:135", 60, 135000, 594000},
    {"Modeline \"3840x2160_24.00\" 297 3840 5116 5204 5500 2160 2168 2178 2250 +HSync +VSync", "3840x2160@24Hz 64:27", 24, 54000, 297000},
    {"Modeline \"3840x2160_25.00\" 297 3840 4896 4984 5280 2160 2168 2178 2250 +HSync +VSync", "3840x2160@25Hz 64:27", 25, 56250, 297000},
    {"Modeline \"3840x2160_30.00\" 297 3840 4016 4104 4400 2160 2168 2178 2250 +HSync +VSync", "3840x2160@30Hz 64:27", 30, 67500, 297000},
    {"Modeline \"3840x2160_50.00\" 594 3840 4896 4984 5280 2160 2168 2178 2250 +HSync +VSync", "3840x2160@50Hz 64:27", 50, 112500, 594000},
    {"Modeline \"3840x2160_60.00\" 594 3840 4016 4104 4400 2160 2168 2178 2250 +HSync +VSync", "3840x2160@60Hz 64:27", 60, 135000, 594000},
    {"Modeline \"1280x720_48.00\" 90 1280 2240 2280 2500 720 725 730 750 +HSync +VSync", "1280x720@48Hz 16:9", 48, 36000, 90000},
    {"Modeline \"1280x720_48.00\" 90 1280 2240 2280 2500 720 725 730 750 +HSync +VSync", "1280x720@48Hz 64:27", 48, 36000, 90000},
    {"Modeline \"1680x720_48.00\" 99 1680 2490 2530 2750 720 725 730 750 +HSync +VSync", "1680x720@48Hz 64:27", 48, 36000, 99000},
    /* VIC 111 */
    {"Modeline \"1920x1080_48.00\" 148.5 1920 2558 2602 2750 1080 1084 1089 1125 +HSync +VSync", "1920x1080@48Hz 16:9", 48, 54000, 148500},
    {"Modeline \"1920x1080_48.00\" 148.5 1920 2558 2602 2750 1080 1084 1089 1125 +HSync +VSync", "1920x1080@48Hz 64:27", 48, 54000, 148500},
    {"Modeline \"2560x1080_48.00\" 198 2560 3558 3602 3750 1080 1084 1089 1100 +HSync +VSync", "2560x1080@48Hz 64:27", 48, 52800, 198000},
    {"Modeline \"3840x2160_48.00\" 594 3840 5116 5204 5500 2160 2168 2178 2250 +HSync +VSync", "3840x2160@48Hz 16:9", 48, 108000, 594000},
    {"Modeline \"4096x2160_48.00\" 594 4096 5116 5204 5500 2160 2168 2178 2250 +HSync +VSync", "4096x2160@48Hz 256:135", 48, 108000, 594000},
    {"Modeline \"3840x2160_48.00\" 594 3840 5116 5204 5500 2160 2168 2178 2250 +HSync +VSync", "3840x2160@48Hz 64:27", 48, 108000, 594000},
    {"Modeline \"3840x2160_100.00\" 1188 3840 4896 4984 5280 2160 2168 2178 2250 +HSync +VSync", "3840x2160@100Hz 16:9", 100, 225000, 1188000},
    {"Modeline \"3840x2160_120.00\" 1188 3840 4016 4104 4400 2160 2168 2178 2250 +HSync +VSync", "3840x2160@120Hz 16:9", 120, 270000, 1188000},
    {"Modeline \"3840x2160_100.00\" 1188 3840 4896 4984 5280 2160 2168 2178 2250 +HSync +VSync", "3840x2160@100Hz 64:27", 100, 225000, 1188000},
    {"Modeline \"3840x2160_120.00\" 1188 3840 4016 4104 4400 2160 2168 2178 2250 +HSync +VSync", "3840x2160@120Hz 64:27", 120, 270000, 1188000},
    /* VIC 121 */
    {"Modeline \"5120x2160_24.00\" 396 5120 7116 7204 7500 2160 2168 2178 2200 +HSync +VSync", "5120x2160@24Hz 64:27", 24, 52800, 396000},
    {"Modeline \"5120x2160_25.00\" 396 5120 6816 6904 7200 2160 2168 2178 2200 +HSync +VSync", "5120x2160@25Hz 64:27", 25, 55000, 396000},
    {"Modeline \"5120x2160_30.00\" 396 5120 5784 5872 6000 2160 2168 2178 2200 +HSync +VSync", "5120x2160@30Hz 64:27", 30, 66000, 396000},
    {"Modeline \"5120x2160_48.00\" 742.5 5120 5866 5954 6250 2160 2168 2178 2475 +HSync +VSync", "5120x2160@48Hz 64:27", 48, 118800, 742500},
    {"Modeline \"5120x2160_50.00\" 742.5 5120 6216 6304 6600 2160 2168 2178 2250 +HSync +VSync", "5120x2160@50Hz 64:27", 50, 112500, 742500},
    {"Modeline \"5120x2160_60.00\" 742.5 5120 5284 5372 5500 2160 2168 2178 2250 +HSync +VSync", "5120x2160@60Hz 64:27", 60, 135000, 742500},
    {"Modeline \"5120x2160_100.00\" 1485 5120 6216 6304 6600 2160 2168 2178 2250 +HSync +VSync", "5120x2160@100Hz 64:27", 100, 225000, 1485000},
};

static struct edid_cta_mode edid_cta_modes2[] = {
    /* VIC 193 */
    {"Modeline \"5120x2160_120.00\" 1485 5120 5284 5372 5500 2160 2168 2178 2250 +HSync +VSync", "5120x2160@120Hz 64:27", 120, 270000, 1485000},
    {"Modeline \"7680x4320_24.00\" 1188 7680 10232 10408 11000 4320 4336 4356 4500 +HSync +VSync", "7680x4320@24Hz 16:9", 24, 108000, 1188000},
    {"Modeline \"7680x4320_25.00\" 1188 7680 10032 10208 10800 4320 4336 4356 4400 +HSync +VSync", "7680x4320@25Hz 16:9", 25, 110000, 1188000},
    {"Modeline \"7680x4320_30.00\" 1188 7680 8232 8408 9000 4320 4336 4356 4400 +HSync +VSync", "7680x4320@30Hz 16:9", 30, 132000, 1188000},
    {"Modeline \"7680x4320_48.00\" 2376 7680 10232 10408 11000 4320 4336 4356 4500 +HSync +VSync", "7680x4320@48Hz 16:9", 48, 216000, 2376000},
    {"Modeline \"7680x4320_50.00\" 2376 7680 10032 10208 10800 4320 4336 4356 4400 +HSync +VSync", "7680x4320@50Hz 16:9", 50, 220000, 2376000},
    {"Modeline \"7680x4320_60.00\" 2376 7680 8232 8408 9000 4320 4336 4356 4400 +HSync +VSync", "7680x4320@60Hz 16:9", 60, 264000, 2376000},
    {"Modeline \"7680x4320_100.00\" 4752 7680 9792 9968 10560 4320 4336 4356 4500 +HSync +VSync", "7680x4320@100Hz 16:9", 100, 450000, 4752000},
    /* VIC 201 */
    {"Modeline \"7680x4320_120.00\" 4752 7680 8032 8208 8800 4320 4336 4356 4500 +HSync +VSync", "7680x4320@120Hz 16:9", 120, 540000, 4752000},
    {"Modeline \"7680x4320_24.00\" 1188 7680 10232 10408 11000 4320 4336 4356 4500 +HSync +VSync", "7680x4320@24Hz 64:27", 24, 108000, 1188000},
    {"Modeline \"7680x4320_25.00\" 1188 7680 10032 10208 10800 4320 4336 4356 4400 +HSync +VSync", "7680x4320@25Hz 64:27", 25, 110000, 1188000},
    {"Modeline \"7680x4320_30.00\" 1188 7680 8232 8408 9000 4320 4336 4356 4400 +HSync +VSync", "7680x4320@30Hz 64:27", 30, 132000, 1188000},
    {"Modeline \"7680x4320_48.00\" 2376 7680 10232 10408 11000 4320 4336 4356 4500 +HSync +VSync", "7680x4320@48Hz 64:27", 48, 216000, 2376000},
    {"Modeline \"7680x4320_50.00\" 2376 7680 10032 10208 10800 4320 4336 4356 4400 +HSync +VSync", "7680x4320@50Hz 64:27", 50, 220000, 2376000},
    {"Modeline \"7680x4320_60.00\" 2376 7680 8232 8408 9000 4320 4336 4356 4400 +HSync +VSync", "7680x4320@60Hz 64:27", 60, 264000, 2376000},
    {"Modeline \"7680x4320_100.00\" 4752 7680 9792 9968 10560 4320 4336 4356 4500 +HSync +VSync", "7680x4320@100Hz 64:27", 100, 450000, 4752000},
    {"Modeline \"7680x4320_120.00\" 4752 7680 8032 8208 8800 4320 4336 4356 4500 +HSync +VSync", "7680x4320@120Hz 64:27", 120, 540000, 4752000},
    {"Modeline \"10240x4320_24.00\" 1485 10240 11732 11908 12500 4320 4336 4356 4950 +HSync +VSync", "10240x4320@24Hz 64:27", 24, 118800, 1485000},
    /* VIC 211 */
    {"Modeline \"10240x4320_25.00\" 1485 10240 12732 12908 13500 4320 4336 4356 4400 +HSync +VSync", "10240x4320@25Hz 64:27", 25, 110000, 1485000},
    {"Modeline \"10240x4320_30.00\" 1485 10240 10528 10704 11000 4320 4336 4356 4500 +HSync +VSync", "10240x4320@30Hz 64:27", 30, 135000, 1485000},
    {"Modeline \"10240x4320_48.00\" 2970 10240 11732 11908 12500 4320 4336 4356 4950 +HSync +VSync", "10240x4320@48Hz 64:27", 48, 237600, 2970000},
    {"Modeline \"10240x4320_50.00\" 2970 10240 12732 12908 13500 4320 4336 4356 4400 +HSync +VSync", "10240x4320@50Hz 64:27", 50, 220000, 2970000},
    {"Modeline \"10240x4320_60.00\" 2970 10240 10528 10704 11000 4320 4336 4356 4500 +HSync +VSync", "10240x4320@60Hz 64:27", 60, 270000, 2970000},
    {"Modeline \"10240x4320_100.00\" 5940 10240 12432 12608 13200 4320 4336 4356 4500 +HSync +VSync", "10240x4320@100Hz 64:27", 100, 450000, 5940000},
    {"Modeline \"10240x4320_120.00\" 5940 10240 10528 10704 11000 4320 4336 4356 4500 +HSync +VSync", "10240x4320@120Hz 64:27", 120, 540000, 5940000},
    {"Modeline \"4096x2160_100.00\" 1188 4096 4896 4984 5280 2160 2168 2178 2250 +HSync +VSync", "4096x2160@100Hz 256:135", 100, 225000, 1188000},
    {"Modeline \"4096x2160_120.00\" 1188 4096 4184 4272 4400 2160 2168 2178 2250 +HSync +VSync", "4096x2160@120Hz 256:135", 120, 270000, 1188000},
};

static const struct edid_cta_mode *vic_to_mode(unsigned char vic)
{
    if (vic > 0 && vic <= ARRAY_SIZE(edid_cta_modes1))
        return edid_cta_modes1 + vic - 1;
    if (vic >= 193 && vic <= ARRAY_SIZE(edid_cta_modes2) + 193)
        return edid_cta_modes2 + vic - 193;
    return NULL;
}

struct dmt_videomode
{
    unsigned int dmt_id;
    unsigned int std_2byte_code;
    unsigned int cvt_3byte_code;
    const char *mode;
};
const char *vesa_mode[] = {
    // 640x350 @ 85Hz (VESA) hsync: 37.9kHz
    "ModeLine \"640x350_85.00\"    31.5  640  672  736  832    350  382  385  445 +hsync -vsync", // 0

    // 640x400 @ 85Hz (VESA) hsync: 37.9kHz
    "ModeLine \"640x400_85.00\"    31.5  640  672  736  832    400  401  404  445 -hsync +vsync",

    // 720x400 @ 85Hz (VESA) hsync: 37.9kHz
    "ModeLine \"720x400_85.00\"    35.5  720  756  828  936    400  401  404  446 -hsync +vsync",

    // 640x480 @ 60Hz (Industry standard) hsync: 31.5kHz
    "ModeLine \"640x480_60.00\"    25.175 640  656  752  800    480  490  492  525 -hsync -vsync",

    // 640x480 @ 72Hz (VESA) hsync: 37.9kHz
    "ModeLine \"640x480_72.00\"    31.5  640  664  704  832    480  489  492  520 -hsync -vsync",

    // 640x480 @ 75Hz (VESA) hsync: 37.5kHz
    "ModeLine \"640x480_75.00\"    31.5  640  656  720  840    480  481  484  500 -hsync -vsync",

    // 640x480 @ 85Hz (VESA) hsync: 43.3kHz
    "ModeLine \"640x480_85.00\"    36.0  640  696  752  832    480  481  484  509 -hsync -vsync",

    // 800x600 @ 56Hz (VESA) hsync: 35.2kHz
    "ModeLine \"800x600_56.00\"    36.0  800  824  896 1024    600  601  603  625 +hsync +vsync",

    // 800x600 @ 60Hz (VESA) hsync: 37.9kHz
    "ModeLine \"800x600_60.00\"    40.0  800  840  968 1056    600  601  605  628 +hsync +vsync",

    // 800x600 @ 72Hz (VESA) hsync: 48.1kHz
    "ModeLine \"800x600_72.00\"    50.0  800  856  976 1040    600  637  643  666 +hsync +vsync",

    // 800x600 @ 75Hz (VESA) hsync: 46.9kHz
    "ModeLine \"800x600_75.00\"    49.5  800  816  896 1056    600  601  604  625 +hsync +vsync",

    // 800x600 @ 85Hz (VESA) hsync: 53.7kHz
    "ModeLine \"800x600_85.00\"    56.3  800  832  896 1048    600  601  604  631 +hsync +vsync",

    // 1024x768 @ 60Hz (VESA) hsync: 48.4kHz
    "ModeLine \"1024x768_60.00\"   65.0 1024 1048 1184 1344    768  771  777  806 -hsync -vsync",

    // 1024x768 @ 70Hz (VESA) hsync: 56.5kHz
    "ModeLine \"1024x768_70.00\"   75.0 1024 1048 1184 1328    768  771  777  806 -hsync -vsync",

    // 1024x768 @ 75Hz (VESA) hsync: 60.0kHz
    "ModeLine \"1024x768_75.00\"   78.75 1024 1040 1136 1312    768  769  772  800 +hsync +vsync",

    // 1024x768 @ 85Hz (VESA) hsync: 68.7kHz
    "ModeLine \"1024x768_85.00\"   94.5 1024 1072 1168 1376    768  769  772  808 +hsync +vsync",

    // 1152x864 @ 75Hz (VESA) hsync: 67.5kHz
    "ModeLine \"1152x864_75.00\"  108.0 1152 1216 1344 1600    864  865  868  900 +hsync +vsync",

    // 1280x960 @ 60Hz (VESA) hsync: 60.0kHz
    "ModeLine \"1280x960_60.00\"  108.0 1280 1376 1488 1800    960  961  964 1000 +hsync +vsync",

    // 1280x960 @ 85Hz (VESA) hsync: 85.9kHz
    "ModeLine \"1280x960_85.00\"  148.5 1280 1344 1504 1728    960  961  964 1011 +hsync +vsync",

    // 1280x1024 @ 60Hz (VESA) hsync: 64.0kHz
    "ModeLine \"1280x1024_60.00\" 108.0 1280 1328 1440 1688   1024 1025 1028 1066 +hsync +vsync", // 19

    // 1280x1024 @ 75Hz (VESA) hsync: 80.0kHz
    "ModeLine \"1280x1024_75.00\" 135.0 1280 1296 1440 1688   1024 1025 1028 1066 +hsync +vsync", // 20

    // 1280x1024 @ 85Hz (VESA) hsync: 91.1kHz
    "ModeLine \"1280x1024_85.00\" 157.5 1280 1344 1504 1728   1024 1025 1028 1072 +hsync +vsync", // 21

    "Modeline \"1280x1024_120.00\" 187.25 1280 1328 1360 1440 1024 1027 1034 1084 +HSync -VSync", // 22

    "Modeline \"1360x768_60.00\" 85.5 1360 1424 1536 1792 768 771 777 795 +HSync +VSync", // 23

    "Modeline \"1360x768_120.00\" 148.25 1360 1408 1440 1520 768 771 781 813 +HSync -VSync", // 24

    "Modeline \"1366x768_60.00\" 85.5 1366 1436 1579 1792 768 771 774 798 +HSync +VSync",

    "Modeline \"1366x768_60.00\" 72 1366 1380 1436 1500 768 769 772 800 +HSync +VSync",

    "Modeline \"1400x1050_60.00\" 101 1400 1448 1480 1560 1050 1053 1057 1080 +HSync -VSync",

    "Modeline \"1400x1050_60.00\" 121.75 1400 1488 1632 1864 1050 1053 1057 1089 -HSync +VSync",

    "Modeline \"1400x1050_75.00\" 156 1400 1504 1648 1896 1050 1053 1057 1099 -HSync +VSync",

    "Modeline \"1400x1050_85.00\" 179.5 1400 1504 1656 1912 1050 1053 1057 1105 -HSync +VSync",

    "Modeline \"1400x1050_120.00\" 208 1400 1448 1480 1560 1050 1053 1057 1112 +HSync -VSync",

    "Modeline \"1440x900_60.00\" 88.75 1440 1488 1520 1600 900 903 909 926 +HSync -VSync",

    "Modeline \"1440x900_60.00\" 106.5 1440 1520 1672 1904 900 903 909 934 -HSync +VSync",

    "Modeline \"1440x900_75.00\" 136.75 1440 1536 1688 1936 900 903 909 942 -HSync +VSync",

    "Modeline \"1440x900_85.00\" 157 1440 1544 1696 1952 900 903 909 948 -HSync +VSync",

    "Modeline \"1440x900_120.00\" 182.75 1440 1488 1520 1600 900 903 909 953 +HSync -VSync",

    "Modeline \"1600x900_60.00\" 108 1600 1624 1704 1800 900 901 904 1000 +HSync +VSync",

    "Modeline \"1680x1050_60.00\" 119 1680 1728 1760 1840 1050 1053 1059 1080 +HSync -VSync",

    "Modeline \"1680x1050_60.00\" 146.25 1680 1784 1960 2240 1050 1053 1059 1089 -HSync +VSync",

    "Modeline \"1680x1050_75.00\" 187 1680 1800 1976 2272 1050 1053 1059 1099 -HSync +VSync",

    "Modeline \"1680x1050_85.00\" 214.75 1680 1808 1984 2288 1050 1053 1059 1105 -HSync +VSync",

    "Modeline \"1680x1050_120.00\" 245.5 1680 1728 1760 1840 1050 1053 1059 1112 +HSync -VSync",

    "Modeline \"1920x1080_60.00\" 148.5 1920 2008 2052 2200 1080 1084 1089 1125 +HSync +VSync",

    // 720x400 @ 70Hz (VESA) hsync: 31.467kHz
    "Modeline \"720x400_70.00\"  28.32  720 736 808 896  400 401 404 417 -hsync -vsync",

    // 720x400 @ 88Hz (VESA) hsync: 31.467kHz

    "Modeline \"720x400_88.00\"  35.5  720 738 846 900 400 421 423 449 -hsync -vsync",

    // total ?

};

const struct dmt_videomode dmt_modes[DMT_SIZE] = {
    {0x01, 0x0000, 0x000000, "Modeline \"640x350_85.00\" 31.5 640 672 736 832 350 382 385 445 +HSync -VSync"},
    {0x02, 0x3119, 0x000000, "Modeline \"640x400_85.00\" 31.5 640 672 736 832 400 401 404 445 -HSync +VSync"},
    {0x03, 0x0000, 0x000000, "Modeline \"720x400_85.00\" 35.5 720 756 828 936 400 401 404 446 -HSync +VSync"},
    {0x04, 0x3140, 0x000000, "Modeline \"640x480_60.00\" 25.175 640 648 744 800 480 482 484 525 -HSync -VSync"},
    {0x05, 0x314c, 0x000000, "Modeline \"640x480_72.00\" 31.5 640 656 696 832 480 481 484 520 -HSync -VSync"},
    {0x06, 0x314f, 0x000000, "Modeline \"640x480_75.00\" 31.5 640 656 720 840 480 481 484 500 -HSync -VSync"},
    {0x07, 0x3159, 0x000000, "Modeline \"640x480_85.00\" 36 640 696 752 832 480 481 484 509 -HSync -VSync"},
    {0x08, 0x0000, 0x000000, "Modeline \"800x600_56.00\" 36 800 824 896 1024 600 601 603 625 +HSync +VSync"},
    {0x09, 0x4540, 0x000000, "Modeline \"800x600_60.00\" 40 800 840 968 1056 600 601 605 628 +HSync +VSync"},
    {0x0a, 0x454c, 0x000000, "Modeline \"800x600_72.00\" 50 800 856 976 1040 600 637 643 666 +HSync +VSync"},
    {0x0b, 0x454f, 0x000000, "Modeline \"800x600_75.00\" 49.5 800 816 896 1056 600 601 604 625 +HSync +VSync"},
    {0x0c, 0x4559, 0x000000, "Modeline \"800x600_85.00\" 56.25 800 832 896 1048 600 601 604 631 +HSync +VSync"},
    {0x0d, 0x0000, 0x000000, "Modeline \"800x600_120.00\" 73.25 800 848 880 960 600 603 607 636 +HSync -VSync"},
    {0x0e, 0x0000, 0x000000, "Modeline \"848x480_60.00\" 33.75 848 864 976 1088 480 486 494 517 +HSync +VSync"},
    {0x0f, 0x0000, 0x000000, NULL},
    {0x10, 0x6140, 0x000000, "Modeline \"1024x768_60.00\" 65 1024 1048 1184 1344 768 771 777 806 -HSync -VSync"},
    {0x11, 0x614a, 0x000000, "Modeline \"1024x768_70.00\" 75 1024 1048 1184 1328 768 771 777 806 -HSync -VSync"},
    {0x12, 0x614f, 0x000000, "Modeline \"1024x768_75.00\" 78.75 1024 1040 1136 1312 768 769 772 800 +HSync +VSync"},
    {0x13, 0x6159, 0x000000, "Modeline \"1024x768_85.00\" 94.5 1024 1072 1168 1376 768 769 772 808 +HSync +VSync"},
    {0x14, 0x0000, 0x000000, "Modeline \"1024x768_120.00\" 115.5 1024 1072 1104 1184 768 771 775 813 +HSync -VSync"},
    {0x15, 0x714f, 0x000000, "Modeline \"1152x864_75.00\" 108 1152 1216 1344 1600 864 865 868 900 +HSync +VSync"},
    {0x55, 0x81c0, 0x000000, "Modeline \"1280x720_60.00\" 74.25 1280 1390 1430 1650 720 725 730 750 +HSync +VSync"},
    {0x16, 0x0000, 0x7f1c21, "Modeline \"1280x768_60.00\" 68.25 1280 1328 1360 1440 768 771 778 790 +HSync -VSync"},
    {0x17, 0x0000, 0x7f1c28, "Modeline \"1280x768_60.00\" 79.5 1280 1344 1472 1664 768 771 778 798 -HSync +VSync"},
    {0x18, 0x0000, 0x7f1c44, "Modeline \"1280x768_75.00\" 102.25 1280 1360 1488 1696 768 771 778 805 -HSync +VSync"},
    {0x19, 0x0000, 0x7f1c62, "Modeline \"1280x768_85.00\" 117.5 1280 1360 1496 1712 768 771 778 809 -HSync +VSync"},
    {0x1a, 0x0000, 0x000000, "Modeline \"1280x768_120.00\" 140.25 1280 1328 1360 1440 768 771 778 813 +HSync -VSync"},
    {0x1b, 0x0000, 0x8f1821, "Modeline \"1280x800_60.00\" 71 1280 1328 1360 1440 800 803 809 823 +HSync -VSync"},
    {0x1c, 0x8100, 0x8f1828, "Modeline \"1280x800_60.00\" 83.5 1280 1352 1480 1680 800 803 809 831 -HSync +VSync"},
    {0x1d, 0x810f, 0x8f1844, "Modeline \"1280x800_75.00\" 106.5 1280 1360 1488 1696 800 803 809 838 -HSync +VSync"},
    {0x1e, 0x8119, 0x8f1862, "Modeline \"1280x800_85.00\" 122.5 1280 1360 1496 1712 800 803 809 843 -HSync +VSync"},
    {0x1f, 0x0000, 0x000000, "Modeline \"1280x800_120.00\" 146.25 1280 1328 1360 1440 800 803 809 847 +HSync -VSync"},
    {0x20, 0x8140, 0x000000, "Modeline \"1280x960_60.00\" 108 1280 1376 1488 1800 960 961 964 1000 +HSync +VSync"},
    {0x21, 0x8159, 0x000000, "Modeline \"1280x960_85.00\" 148.5 1280 1344 1504 1728 960 961 964 1011 +HSync +VSync"},
    {0x22, 0x0000, 0x000000, "Modeline \"1280x960_120.00\" 175.5 1280 1328 1360 1440 960 963 967 1017 +HSync -VSync"},
    {0x23, 0x8180, 0x000000, "Modeline \"1280x1024_60.00\" 108 1280 1328 1440 1688 1024 1025 1028 1066 +HSync +VSync"},
    {0x24, 0x818f, 0x000000, "Modeline \"1280x1024_75.00\" 135 1280 1296 1440 1688 1024 1025 1028 1066 +HSync +VSync"},
    {0x25, 0x8199, 0x000000, "Modeline \"1280x1024_85.00\" 157.5 1280 1344 1504 1728 1024 1025 1028 1072 +HSync +VSync"},
    {0x26, 0x0000, 0x000000, "Modeline \"1280x1024_120.00\" 187.25 1280 1328 1360 1440 1024 1027 1034 1084 +HSync -VSync"},
    {0x27, 0x0000, 0x000000, "Modeline \"1360x768_60.00\" 85.5 1360 1424 1536 1792 768 771 777 795 +HSync +VSync"},
    {0x28, 0x0000, 0x000000, "Modeline \"1360x768_120.00\" 148.25 1360 1408 1440 1520 768 771 781 813 +HSync -VSync"},
    {0x51, 0x0000, 0x000000, "Modeline \"1366x768_60.00\" 85.5 1366 1436 1579 1792 768 771 774 798 +HSync +VSync"},
    {0x56, 0x0000, 0x000000, "Modeline \"1366x768_60.00\" 72 1366 1380 1436 1500 768 769 772 800 +HSync +VSync"},
    {0x29, 0x0000, 0x0c2021, "Modeline \"1400x1050_60.00\" 101 1400 1448 1480 1560 1050 1053 1057 1080 +HSync -VSync"},
    {0x2a, 0x9040, 0x0c2028, "Modeline \"1400x1050_60.00\" 121.75 1400 1488 1632 1864 1050 1053 1057 1089 -HSync +VSync"},
    {0x2b, 0x904f, 0x0c2044, "Modeline \"1400x1050_75.00\" 156 1400 1504 1648 1896 1050 1053 1057 1099 -HSync +VSync"},
    {0x2c, 0x9059, 0x0c2062, "Modeline \"1400x1050_85.00\" 179.5 1400 1504 1656 1912 1050 1053 1057 1105 -HSync +VSync"},
    {0x2d, 0x0000, 0x000000, "Modeline \"1400x1050_120.00\" 208 1400 1448 1480 1560 1050 1053 1057 1112 +HSync -VSync"},
    {0x2e, 0x9500, 0xc11821, "Modeline \"1440x900_60.00\" 88.75 1440 1488 1520 1600 900 903 909 926 +HSync -VSync"},
    {0x2f, 0x9500, 0xc11828, "Modeline \"1440x900_60.00\" 106.5 1440 1520 1672 1904 900 903 909 934 -HSync +VSync"},
    {0x30, 0x950f, 0xc11844, "Modeline \"1440x900_75.00\" 136.75 1440 1536 1688 1936 900 903 909 942 -HSync +VSync"},
    {0x31, 0x9519, 0xc11868, "Modeline \"1440x900_85.00\" 157 1440 1544 1696 1952 900 903 909 948 -HSync +VSync"},
    {0x32, 0x0000, 0x000000, "Modeline \"1440x900_120.00\" 182.75 1440 1488 1520 1600 900 903 909 953 +HSync -VSync"},
    {0x53, 0xa9c0, 0x000000, "Modeline \"1600x900_60.00\" 108 1600 1624 1704 1800 900 901 904 1000 +HSync +VSync"},

    {0x39, 0x0000, 0x0c2821, "Modeline \"1680x1050_60.00\" 119 1680 1728 1760 1840 1050 1053 1059 1080 +HSync -VSync"},

    {0x3a, 0x0b30, 0x0c2828, "Modeline \"1680x1050_60.00\" 146.25 1680 1784 1960 2240 1050 1053 1059 1089 -HSync +VSync"},

    {0x3b, 0x0b3f, 0x0c2844, "Modeline \"1680x1050_75.00\" 187 1680 1800 1976 2272 1050 1053 1059 1099 -HSync +VSync"},

    {0x3c, 0xb319, 0x0c2868, "Modeline \"1680x1050_85.00\" 214.75 1680 1808 1984 2288 1050 1053 1059 1105 -HSync +VSync"},

    {0x3d, 0x0000, 0x000000, "Modeline \"1680x1050_120.00\" 245.5 1680 1728 1760 1840 1050 1053 1059 1112 +HSync -VSync"},

    {0x52, 0xD1C0, 0x000000, "Modeline \"1920x1080_60.00\" 148.5 1920 2008 2052 2200 1080 1084 1089 1125 +HSync +VSync"},
};

int findElementWithField(const char *arr[], int arrSize, const char *field)
{
    for (int i = 0; i < arrSize; i++)
    {
        if (strstr(arr[i], field) != NULL)
        {
            return i;
        }
    }

    return -1;
}

static int get_est_timing(unsigned char *block)
{
    int idx = 0;
    unsigned char c;
    char *est_mode_string = NULL;
    int found = 0;
    c = block[0];
    if (c & 0x80)
    {
        est_mode_string = "720x400_70.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x40)
    {
        est_mode_string = "720x400_88.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x20)
    {
        est_mode_string = "640x480_60.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x10)
    {
        est_mode_string = "640x480_67.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x08)
    {
        est_mode_string = "640x480_72.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x04)
    {
        est_mode_string = "640x480_75.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x02)
    {
        est_mode_string = "800x600_56.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x01)
    {
        est_mode_string = "800x600_60.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    c = block[1];
    if (c & 0x80)
    {
        est_mode_string = "800x600_72.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x40)
    {
        est_mode_string = "800x600_75.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x20)
    {
        est_mode_string = "832x624_75.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x10)
    {
        est_mode_string = "1024x768_87.00 Interlaced";
        est_mode_string = "Not Support!";
    }
    if (c & 0x08)
    {
        est_mode_string = "1024x768_60.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x04)
    {
        est_mode_string = "1024x768_70.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x02)
    {
        est_mode_string = "1024x768_75.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    if (c & 0x01)
    {
        est_mode_string = "1280x1024_75.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    c = block[2];
    if (c & 0x80)
    {
        est_mode_string = "1152x870_75.00";
        idx = findElementWithField(vesa_mode, sizeof(vesa_mode) / sizeof(vesa_mode[0]), est_mode_string);
        if (idx != -1)
            printf("%s\r\n", vesa_mode[idx]);
        found = 1;
    }
    return found;
}

static int get_std_timing(unsigned char *block)
{

    int i;
    char *mode = NULL;
    int found = 0;
    for (i = 0; i < DMT_SIZE; i++)
    {
        unsigned int std_2byte_code = block[0] << 8 | block[1];
        if (std_2byte_code == dmt_modes[i].std_2byte_code)
        {
            found = 1;
            break;
        }
    }

    if (i < DMT_SIZE && dmt_modes[i].mode)
    {
        /* DMT mode found */
        mode = dmt_modes[i].mode;
        printf("%s\r\n", mode);
    }
    return found;
}

static void cta_svd(const unsigned char *x, int n, int for_ycbcr420);
static void cta_video_block(const unsigned char *x, unsigned int length)
{
    cta_svd(x, length, 0);
}
static void cta_svd(const unsigned char *x, int n, int for_ycbcr420)
{
    int i;

    for (i = 0; i < n; i++)
    {
        const struct edid_cta_mode *vicmode = NULL;
        unsigned char svd = x[i];
        unsigned char native;
        unsigned char vic;
        const char *mode;

        if ((svd & 0x7f) == 0)
            continue;

        if ((svd - 1) & 0x40)
        {
            vic = svd;
            native = 0;
        }
        else
        {
            vic = svd & 0x7f;
            native = svd & 0x80;
        }

        vicmode = vic_to_mode(vic);
        if (vicmode)
        {
            mode = vicmode->name;
            // printf("MODE:%s\r\n", mode);
            printf("%s\r\n", vicmode->xmodeline);
        }
        else
        {
            mode = "Unknown mode";
        }
    }
}
static void cta_block(const unsigned char *x)
{
    static int last_block_was_hdmi_vsdb;
    static int have_hf_vsdb, have_hf_scdb;
    static int first_block = 1;
    unsigned int length = x[0] & 0x1f;
    unsigned int oui;

    switch ((x[0] & 0xe0) >> 5)
    {
    case 0x02:
        // printf("  Video Data Block\r\n");
        cta_video_block(x + 1, length);
        break;
    default:
    {
        int tag = (*x & 0xe0) >> 5;
        int length = *x & 0x1f;
        // printf("  Unknown tag %d, length %d (raw %02x)\r\n", tag, length, *x);
        break;
    }
    }
    first_block = 0;
    last_block_was_hdmi_vsdb = 0;
}

static int parse_cta(const unsigned char *x)
{
    int ret = 0;
    int version = x[1];
    int offset = x[2];
    const unsigned char *detailed;

    char *cur_block = "CTA-861";

    if (version >= 1)
        do
        {
            if (version == 1 && x[3] != 0)
                ret = 1;

            if (offset < 4)
                break;

            if (version < 3)
            {
                ;
                ;
                if (offset - 4 > 0)
                    /* do stuff */;
            }

            if (version >= 2)
            {
                ;
                ;
            }
            if (version == 3)
            {
                int i;

                for (i = 4; i < offset; i += (x[i] & 0x1f) + 1)
                {
                    cta_block(x + i);
                }
                printf("\r\n");
            }
        } while (0);

    return ret;
}

int main()
{
    edid_raw_t data;
    unsigned char *block;
    unsigned char *block2;
    int ret = -1;
    memset(&data, 0, sizeof(edid_raw_t));
    ret = lt8618_ioctl_init();
    if (ret)
        perr("lt8618_ioctl_init fail!\r\n");
    ret = lt8618_get_edid_data(&data);
    if (ret)
        perr("lt8618_get_edid_data fail!\r\n");

    // for (size_t i = 0; i < 256; i++)
    // {
    //     printf("0x%02x ", data.edid_data[i]);
    //     if ((i + 1) % 16 == 0)
    //         printf("\r\n");
    // }
    // if (data.block_num == 2)
    // {
    //     for (size_t i = 0; i < 256; i++)
    //     {
    //         printf("0x%x ", data.edid_data2[i]);
    //         if ((i + 1) % 16 == 0)
    //             printf("\r\n");
    //     }
    // }

    // block 1: est timing
    printf("#Automatically generated, please do not edit\r\n");
    printf("#EST Timing\r\n");
    block = data.edid_data + 0x23;
    ret = get_est_timing(block);
    if (!ret)
        perr("get_est_timing fail!\r\n");
    // block 2:std timing
    printf("#STD(DMT) Timing\r\n");
    block = data.edid_data + 0x26;
    for (size_t i = 0; i < 8; i++, block += 2)
    {
        ret = get_std_timing(block);
    }
    if (data.edid_data)
    {
        // extend block?
        block2 = data.edid_data + 128;
        // printf("0X%2X\r\n", block2[0]);
        if (block2[0] == 0x02)
        {
            printf("#CEA Timing\r\n");
            ret = parse_cta(block2);
            if (ret)
            {
                perr("parse_cta fail!\r\n");
            }
        }
    }
}